package com.liao.starapp.luck;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.liao.starapp.R;
import com.liao.starapp.star.Star;
import com.liao.starapp.utils.GetAssetsData;

import java.util.ArrayList;
import java.util.List;

public class ItemLuckGridViewLayoutAdapter extends BaseAdapter {

    private List<Luck> lucks = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemLuckGridViewLayoutAdapter(Context context, ArrayList<Luck> lucks) {
        this.context = context;
        this.lucks = lucks;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return lucks.size();
    }

    @Override
    public Luck getItem(int position) {
        return lucks.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_star_gridview_layout, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((Luck) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(Luck luck, ViewHolder holder) {
        Bitmap bitmap = GetAssetsData.getGridViewImg(context, "xzcontentlogo/" + luck.getLogoname() + ".png");
        holder.imageView.setImageBitmap(bitmap);
        holder.tvName.setText(luck.getName());
    }

    protected class ViewHolder {
        private ImageView imageView;
        private TextView tvName;

        public ViewHolder(View view) {
            imageView = (ImageView) view.findViewById(R.id.imageView);
            tvName = (TextView) view.findViewById(R.id.tv_name);
        }
    }
}
